package com.train.batch.mapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.train.batch.resp.JobPageResp;
import com.train.batch.dto.AddJobDTO;
import com.train.batch.dto.JobPageDTO;
import com.train.batch.po.QrtzJobRecordPO;
import com.train.common.model.page.PageSorted;
import com.train.common.resp.PageResp;
import com.train.common.utils.date.LocalDateTimeUtils;
import com.train.mybatis.base.MyBaseMapper;

import java.util.List;

public interface QrtzJobRecordMapper extends MyBaseMapper<QrtzJobRecordPO> {

    default PageResp<JobPageResp> pageList(JobPageDTO jobPageDTO) {
        PageResp<QrtzJobRecordPO> qrtzJobRecordPOPageResp = this.selectPage(
                jobPageDTO,
                new LambdaQueryWrapper<>(),
                new PageSorted(QrtzJobRecordPO::getUpdateTime),
                new PageSorted(QrtzJobRecordPO::getCreateTime));
        List<JobPageResp> jobPageRespList = BeanUtil.copyToList(qrtzJobRecordPOPageResp.getData(), JobPageResp.class);
        return new PageResp<>(qrtzJobRecordPOPageResp.getTotal(), jobPageRespList);
    }

    default boolean saveJob(AddJobDTO addJobDTO) {
        QrtzJobRecordPO qrtzJobRecordPO = BeanUtil.copyProperties(addJobDTO, QrtzJobRecordPO.class);
        return insert(qrtzJobRecordPO) > 0;
    }

    default boolean editJob(AddJobDTO addJobDTO) {
        LambdaUpdateWrapper<QrtzJobRecordPO> update = new LambdaUpdateWrapper<>();
        update.eq(QrtzJobRecordPO::getJobName, addJobDTO.getJobName())
                .eq(QrtzJobRecordPO::getGroupName, addJobDTO.getGroupName())
                .set(QrtzJobRecordPO::getCronExpress, addJobDTO.getCronExpress())
                .set(ObjectUtil.isNotNull(addJobDTO.getNextFireTime()), QrtzJobRecordPO::getNextFireTime, addJobDTO.getNextFireTime())
                .set(QrtzJobRecordPO::getUpdateTime, LocalDateTimeUtils.getNow());
        return update(update) > 0;
    }

    default boolean editStatusJob(AddJobDTO addJobDTO) {
        LambdaUpdateWrapper<QrtzJobRecordPO> update = new LambdaUpdateWrapper<>();
        update.eq(QrtzJobRecordPO::getJobName, addJobDTO.getJobName())
                .eq(QrtzJobRecordPO::getGroupName, addJobDTO.getGroupName())
                .set(QrtzJobRecordPO::getJobStatus, addJobDTO.getJobStatus())
                .set(QrtzJobRecordPO::getUpdateTime, LocalDateTimeUtils.getNow());
        return update(update) > 0;
    }

    default boolean deleteJob(String jobName, String groupName) {
        LambdaQueryWrapper<QrtzJobRecordPO> delete = new LambdaQueryWrapper<>();
        delete.eq(QrtzJobRecordPO::getJobName, jobName)
                .eq(QrtzJobRecordPO::getGroupName, groupName);
        return delete(delete) > 0;
    }

    default boolean editExecuteTime(AddJobDTO addJobDTO) {
        LambdaUpdateWrapper<QrtzJobRecordPO> update = new LambdaUpdateWrapper<>();
        update.eq(QrtzJobRecordPO::getJobName, addJobDTO.getJobName())
                .eq(QrtzJobRecordPO::getGroupName, addJobDTO.getGroupName())
                .set(QrtzJobRecordPO::getPreFireTime, addJobDTO.getPreFireTime())
                .set(QrtzJobRecordPO::getNextFireTime, addJobDTO.getNextFireTime());
        return update(update) > 0;
    }
}
